var sel_year;
var sel_month;
var sel_date;


// On page load
$(document).ready(function($){
	
	// Room/Date tabs in sidebar
	$("#tabs").tabs({cookie:{expires: 7, name: 'tab.bookings'}});
	
	// Room information box
	$('a[rel*=facebox]').facebox();
	
	/* XHR calls for loading rooms */

	// Room LI element click event
	$('li[class*=room]').bind("click", function(e){
		roomajax($(e.currentTarget).find('a[rel*=room]').attr("href"), $(e.currentTarget));
	});

	// Room A element click event
	$('a[rel*=room]').bind("click", function(e){
		roomajax($(e.currentTarget).attr("href"), $(e.currentTarget).parent());
		return false;
	});
	
	// Set up calendar navigation links (months & dates)
	calnavlinks();
	
	// Navigation header links
	navheaderlinks();
	
});




/* XHR function to load a room timetable into the container */

// Load room timetable via AJAX and set classes on LIs
function roomajax(url, li){
	//$('#tt').html(ajax_load).load(url);
	//$('#tt').load(url);
	crbsajax(url, 'tt', function(){ navheaderlinks(); });
	$('ul.bookings-roomlist li').removeClass("current");
	li.addClass("current");
}



function updatelinks(){
	navheaderlinks();
	calnavlinks();
}


// Set up calendar month navigation links
// In a function as it's called during a callback
function calnavlinks(){
	
	$('a[rel*=calmonth]').bind("click", function(e){
		crbsajax($(e.currentTarget).attr("href"), 'cal', calnavlinks);
		return false;
	});
	$('a[rel*=caldate]').bind("click", function(e){
		crbsajax($(e.currentTarget).attr("href"), 'tt', navheaderlinks);
		$('a[rel*=caldate]').removeClass("current");
		$(e.currentTarget).addClass("current");
		return false;
	});
	//$('a[rel*=caldate]').removeClass("current");
	
}



// Set up javascript for clicking on the links in the bookings area navigating header
function navheaderlinks(){
	
	$('a[rel*=navheader]').bind("click", function(e){
		
		// Send the XHR to load the timetable in the main tt container
		crbsajax($(e.currentTarget).attr("href"), 'tt', updatelinks);
		
		// Get date from ID of link
		seldate = $(e.currentTarget).attr("id");
		seldate = seldate.split('-');
		
		update_cal = false;
		if(seldate[1] != sel_month){
			//console.log('seldate1 is ' + seldate[1]);
			//console.log('sel_date is ' + sel_month);
			// Need to do XHR call later
			update_cal = true;
		}
		
		// Update the Javascript vars to what is chosen
		sel_year = seldate[0];
		sel_month = seldate[1];
		sel_date = seldate[2];
		
		// Convert the 2-digit day number into single (how it's echo'd in the cal)
		daynum = sel_date.replace(/^0/, '');
		
		// Load the calendar month in sidebar
		if(update_cal == true){
			// Months are different, load calendar for chosen month
			url = siteurl + 'bookings/calendar/' + sel_year + '/' + sel_month;
			crbsajax(url, 'cal', function(){
				// On callback, 1) highlight day, and 2) ajaxify the links
				highlight_day(daynum);
				calnavlinks();
			});
		} else {
			// Just highlight the day
			highlight_day(daynum);
		}
		
		// Prevent page from loading
		return false;
		
	});
	
}



/**
 * Highlight a day in the calendar
 */
function highlight_day(daynum, weekstart){
	$('a[rel*=caldate]').removeClass("current");
	$('a#cal_' + daynum).addClass("current");
}
